package qingke.struts.example1.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import qingke.struts.example1.model.People;

public class WebDao {
	private final String driver = "com.mysql.jdbc.Driver";
	private final String url = "jdbc:mysql://localhost:3306/web?useSSL=false";
	private final String u = "root";
	private final String p = "6775";

	private Connection conn = null;
	private String sql;
	private PreparedStatement ps = null;
	private ResultSet rs = null;

	public WebDao() {
	}

	{
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, u, p);
			System.out.println("Connect to database successfully!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public List<People> getAllPeople(){
		List<People> list =new ArrayList<People>();
		sql="select * from people";
		
		try {
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs!=null){
				while(rs.next()){
				   People pp=new People();
				   pp.setId(rs.getInt("id"));
				   pp.setName(rs.getString("name"));
				   pp.setAge(rs.getInt("age"));
				   pp.setHeight(rs.getInt("height"));
				   list.add(pp);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

}
